package android.apps.view;

import android.apps.bll.RawBLL;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;


/**
 * Created by Andrew on 2015/12/28.
 */
public class RawView extends View {
    public RawBLL rawBll = null;
    Paint paint;
    int width = 100;
    int height = 100;
    private static final int  DRAW_OFFSET = 5;
    Point point = new Point(DRAW_OFFSET,DRAW_OFFSET);
    public RawView(Context context) {
        super(context);
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    }

    public RawView(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        width = w;
        height = h;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(null != rawBll)
        {
            Bitmap bitmap = Bitmap.createBitmap(rawBll.rgbBuffer, RawBLL.LEPTON_WIDTH, RawBLL.LEPTON_HEIGHT, Bitmap.Config.ARGB_8888);

            drawImage(canvas,bitmap,0,0,RawBLL.LEPTON_WIDTH,RawBLL.LEPTON_HEIGHT,0,0,width,height);
            Point ptSrc = new Point(point.x * RawBLL.LEPTON_WIDTH / width,point.y * RawBLL.LEPTON_HEIGHT / height);
            String strTmp = Float.toString(rawBll.getTemp(ptSrc.x, ptSrc.y));
            Paint pt = new Paint();
            pt.setTextSize(30);
            //pt1.setTextAlign(Paint.Align.LEFT);
            pt.setColor(Color.BLUE);
            canvas.drawText(strTmp,point.x,point.y,pt);
        }
        else{
            canvas.drawColor(Color.CYAN);
        }
        invalidate();
    }



    void drawImage(Canvas canvas, Bitmap blt, int srcX, int srcY, int srcW, int srcH, int destX, int destY,int destW,int destH)
    {                                                       //x,y表示绘画的起点，
        Rect src = new Rect();
        Rect dst = new Rect();
        src.left = srcX;
        src.top = srcY;
        src.right = srcX + srcW;
        src.bottom = srcY + srcH;

        dst.left =  destX;
        dst.top = destY;
        dst.right = destX + destW;
        dst.bottom = destY + destH;
        Paint pt = new Paint();
        pt.setAntiAlias(true);
        pt.setFilterBitmap(true);
        canvas.drawBitmap(blt, src, dst, pt);//这个方法  第一个参数是图片，第二个参数是 绘画该图片需显示多少。也就是说你想绘画该图片的某一些地方，而不是全部图片，第三个参数表示该图片绘画的位置

        src = null;
        dst = null;
    }

    public void myTouch(Point pt)
    {
        point = pt;
    }
}
